CREATE proc [dbo].[asi_ResultPage]
@select nvarchar(4000),
@offset int = 0,
@pageSize int = 10,
@returnRowCount bit = 0 As
begin
declare @countSelect nvarchar(4000)
declare @limiter int
declare @posFrom int
declare @posOrderBy int
declare @posSelectList int
declare @tmp nvarchar(4000)
declare @tmpTable char(38)
declare @tmpTable2 char(38)
declare @tmpTable3 char(38)
declare @upperSelect nvarchar(4000)
set @limiter = 500
if (@offset) >= (@limiter - @pageSize)
begin
set @limiter = @offset + (@pageSize * 2)
end
set @tmpTable = N'#z' + replace(cast(newid() As nchar(36)),N'-',N'')
set @tmpTable2 = N'#z' + replace(cast(newid() As nchar(36)),N'-',N'')
set @tmpTable3 = N'#z' + replace(cast(newid() as nchar(36)),N'-',N'')
set @select = ltrim(@select)
set @select = replace(@select,nchar(9),N' ')
set @select = replace(@select,nchar(10),N' ')
set @select = replace(@select,nchar(13),N' ')
set @upperSelect = upper(@select)
if charindex(' DISTINCT ', @upperSelect) = 0
begin
set @posSelectList = charindex(N' ', @upperSelect) + 1
end
else
begin
set @posSelectList = charindex(N' DISTINCT ', @upperSelect) + 10
end
set @posFrom = charindex(N' FROM ', @upperSelect)
set @posOrderBy = charindex(N' ORDER BY ', @upperSelect)
set @countSelect = @select
if @posOrderBy > 0
begin
set @countSelect = left(@countSelect, @posOrderBy)
end
set @countSelect = stuff(@countSelect, @posSelectList, @posFrom - @posSelectList, N' count(*) AS Row_Count ')
if @offset = 0 or @pageSize = 0
begin
set @tmp = @select
set @tmp = stuff(@tmp,@posSelectList,0,N'cast(0 as int) As ResultRow,')
if @pageSize > 0
begin
set @tmp = stuff(@tmp,@posSelectList,0,N'TOP ' + cast(@pageSize As varchar(10)) + N' ')
end
set @tmp = N'set nocount on'+nchar(13)+nchar(10)+@tmp
if @returnRowCount <> 0 and @pageSize = 0
begin
set @tmp = @tmp+nchar(13)+nchar(10)+N'select @@ROWCOUNT AS Row_Count'
end
if @returnRowCount <> 0 and @pageSize <> 0
begin
set @countSelect = @select
if @posOrderBy > 0
begin
set @countSelect = left(@countSelect,@posOrderBy)
end
set @countSelect = stuff(@countSelect,@posSelectList,@posFrom - @posSelectList,N' TOP '+cast(@limiter As varchar(10))+N' 0 AS rc INTO '+@tmpTable3)
set @countSelect = @countSelect+nchar(13)+nchar(10)+N'select @@ROWCOUNT AS Row_Count'
set @countSelect = @countSelect+nchar(13)+nchar(10)+N'drop table '+@tmpTable3
set @tmp = @tmp+nchar(13)+nchar(10)+@countSelect
end
set @tmp = @tmp+nchar(13)+nchar(10)+N'set nocount off'
end
else
begin
set @tmp = @select
set @tmp = stuff(@tmp,@posFrom,0,N' into '+@tmpTable)
set @tmp = stuff(@tmp,@posSelectList,0,N'top '+convert(nvarchar(10),(@offset+@pageSize))+N' ')
set @tmp = N'set nocount on'+nchar(13)+nchar(10)+@tmp
set @tmp = @tmp+nchar(13)+nchar(10)+N'select identity(int,1,1) As ResultRow,* into '+@tmpTable2+N' from '+@tmpTable
set @tmp = @tmp+nchar(13)+nchar(10)+N'drop table '+@tmpTable
set @tmp = @tmp+nchar(13)+nchar(10)+N'set rowcount '+cast(@pageSize As varchar(10))
set @tmp = @tmp+nchar(13)+nchar(10)+N'select * from '+@tmpTable2+N' where ResultRow > '+convert(nvarchar(10),@offset)
set @tmp = @tmp+nchar(13)+nchar(10)+N'set rowcount 0'
set @tmp = @tmp+nchar(13)+nchar(10)+N'drop table '+@tmpTable2
if @returnRowCount <> 0
begin
set @tmp = @tmp+nchar(13)+nchar(10)+@countSelect
end
set @tmp = @tmp+nchar(13)+nchar(10)+N'set nocount off'
end
exec(@tmp)
end
GO